﻿using Microsoft.AspNetCore.Mvc.Filters;
using System.Net.Sockets;
using System.Net;

namespace S_Incident.API.Extensions
{
    public class MyExceptionFilter : IExceptionFilter
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<MyExceptionFilter> logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        public MyExceptionFilter(ILogger<MyExceptionFilter> logger)
        {
            this.logger = logger;
        }

        public void OnException(ExceptionContext context)
        {

            var hostNmae = Dns.GetHostName();
            var hostEntry = Dns.GetHostEntry(hostNmae);
            var IpAddress = hostEntry.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);

            //写日志
            logger.LogError(IpAddress + context.Exception.StackTrace[..50] + "赵小安你咋又报错了呢");
            //异常解决
            context.ExceptionHandled = true;
        }
    }

}
